package www.xpu.edu.app.job;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import www.xpu.edu.app.Activity_job;
import www.xpu.edu.app.Adapter_job6;
import www.xpu.edu.app.Adapter_job6_2;
import www.xpu.edu.app.Job3Bean;
import www.xpu.edu.app.Job6;
import www.xpu.edu.app.Job6Bean;
import www.xpu.edu.app.R;

public class Activity_job6 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //职位管理
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job6);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Button button1=findViewById(R.id.title_back);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Button button2=findViewById(R.id.title_change);
        button2.setVisibility(View.INVISIBLE);
        TextView text=findViewById(R.id.title_text);
        text.setText("职位管理");
        SharedPreferences pref=getSharedPreferences("num",MODE_PRIVATE);
        SharedPreferences pref2=getSharedPreferences("job6",MODE_PRIVATE);
        String num = pref.getString("num", "");
        System.out.println(num);
        sendRequest(num);
        String data = pref2.getString("data", "");
        System.out.println(data);
        List<Job6Bean> job6Beans=new ArrayList<>();
        Gson gson=new Gson();
        job6Beans=gson.fromJson(data,new TypeToken<List<Job6Bean>>(){}.getType());


        List<Job6> job6=new ArrayList<>();
        if(job6Beans==null)
        {
            //饮鸠止渴法
            startActivity(new Intent(Activity_job6.this,Activity_job6.class));
        }
        for (int i = 0; i < job6Beans.size(); i++) {
            job6.add(new Job6(job6Beans.get(i).getJobbname(),job6Beans.get(i).getAddress(),job6Beans.get(i).getWage(),job6Beans.get(i).getTime()));
        }
        /*for (int i = 0; i < 10; i++) {
            job6.add(new Job6("安全攻防工程师","北京","10-15k","今天"));
        }*/
        ListView listView=findViewById(R.id.job6_list);
        Adapter_job6_2 adapter_job6 = new Adapter_job6_2(Activity_job6.this, R.layout.adapter_job6, job6);
        listView.setAdapter(adapter_job6);
        final List<Job6Bean> finalJob6Beans = job6Beans;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent();
                intent.setClass(Activity_job6.this,Activity_job7.class);
                ArrayList<String> list=new ArrayList<>();
                list.add(finalJob6Beans.get(position).getCorname());
                list.add(finalJob6Beans.get(position).getType());
                list.add(finalJob6Beans.get(position).getAddress());
                list.add(finalJob6Beans.get(position).getJobbname());
                list.add(finalJob6Beans.get(position).getExp());
                list.add(finalJob6Beans.get(position).getWage());
                list.add(finalJob6Beans.get(position).getGrad());
                list.add(finalJob6Beans.get(position).getRequ());
                intent.putStringArrayListExtra("data",list);

                startActivity(intent);
            }
        });

    }
    private void sendRequest(final String num)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            //样例:http://188.131.250.4:8080/xpu/Select_recruit2?num=19819981998
                            /*http://192.168.43.188:8080/test2/resume*/
                            /*http://192.168.123.196:8080/test2/resume*/
                            //.url("http://192.168.43.188:8080/test2/resume")
                            .url("http://188.131.250.4:8080/xpu/Select_recruit2?num="+num)
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    SharedPreferences.Editor editor=getSharedPreferences("job6",MODE_PRIVATE).edit();
                    // System.out.println("job3的内容【服务器端获取】->"+responseData);
                    editor.putString("data",responseData);
                    editor.apply();



                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
}

